package previous.Test_0301;

/**
 * Created with Intellij IDEA
 * Description:
 * User: 星辰
 */
 class TreeNode {
      int val;
     TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
 }
public class Review {
     //求根节点到叶节点的数字之和
    //
    public int sumNumbers(TreeNode root) {
          return  dfs(root,0);
    }
    public int dfs(TreeNode root,int n){
        n=n*10+root.val;
        if(root.right==null && root.left==null) return n;
        int ret=0;
        if(root.left!=null) ret+=dfs(root.left,n);
        if(root.right!=null) ret+=dfs(root.right,n);
        return ret;
    }
    //二叉树剪枝
    public TreeNode pruneTree(TreeNode root) {
           if(root==null){
               return null;
           }
           if(root.left!=null){
               root.left=pruneTree(root.left);
           }
           if(root.right!=null){
              root.right=pruneTree(root.right);
           }
        if(root.left==null && root.right==null && root.val!=1){
            return null;
        }
           return root;
    }
    //验证二叉搜索树
    public boolean isValidBST(TreeNode root) {
          if(root==null){
              return true;
          }
          if(root.left!=null){
        boolean left=isValidBST(root.left);
          }
          if(root.right!=null){
        boolean right=isValidBST(root.right);
          }

        return root.val>root.left.val==true && root.val<root.right.val==true;
    }
}
